<?php 
//echo $this->render('call_center/datepicker.phtml'); 

// Create an array with key == name of status and value is color
$rowset = OrderStatusPeer::getInstance()->fetchAll();

$color2name = array();
foreach ($rowset as $row) {
	if (!$row->getColor()) continue;
	$color2name[ $row->getName() ] = $row->getColor();
}

?>

<script type="text/javascript">
var $refreshTableTimeout = 30000;

var $columnForSelectStatusClass = $("th[name='order_status_id']");
function indicateColors() {
	$("tr").each(function() {
		var self = $(this);

		var $class = "." + $columnForSelectStatusClass.attr("class");
		var text = self.find($class).find('a').html();
		var cols = self.find("td");

		<?php 
		foreach ($color2name as $name => $color) {
			echo "
			if (text == '$name') {
				cols.css('background-color', '$color');
			}
			";
		}
		?>
	});
}

function refreshTableCallback() {
	indicateColors();

	var $cook = getAutorefresh();
	if ($cook != 'no') {
		clearTimeout($refreshInterval);
		$refreshInterval = setTimeout(refreshTable, $refreshTableTimeout);
	}
}

function autorefresh_click(object) {
	if (object.checked) {
		setAutorefresh('yes');
		$refreshInterval = setTimeout(refreshTable, $refreshTableTimeout);
	} else {
		setAutorefresh('no');
		clearTimeout($refreshInterval);
	}
}

<?php 
$login = Zend_Auth::getInstance()->getIdentity();
?>
function setAutorefresh($flag)
{
	var $login = '<?php echo $login; ?>';
	var $value = $.cookie("ccautorefresh2");
	if (!$value) $value = '';
	var $regexp = new RegExp($login + ':([^,]+)');
	var $match =  $value.match($regexp);
	if ($match && $match[1]) {
		$value = $value.replace($regexp, '');
	}

	$value = $login + ':' + $flag + ',' + $value;
	$.cookie("ccautorefresh2", $value, { expires: 30, path: '/' });
}

function getAutorefresh()
{
	var $value = $.cookie("ccautorefresh2");
	if (!$value) $value = '';
	var $regexp = new RegExp('<?php echo $login; ?>:([^,]+)');
	var $match =  $value.match($regexp);
	if ($match && $match[1]) {
		return $match[1];
	}
}


var $refreshInterval = null;
$(function() {
	indicateColors();

	var $report = $("#report");
	$("#crud_list_items").before($report.html());
	$report.remove();
	//$('input[name*=report[comments]]').attr('checked', '');
	$("#create_report").live("click", function() {
		var $self = $(this);
		var $block = $self.next();
		$block.toggle(500);
		return false;
	});
	
	var $search = $("#search");
	$("#crud_list_items").before($search.html());
	$search.remove();
	//$(".asdate").change(refreshTable);
	$(".asdate").datepicker({dateFormat: 'yy-mm-dd'});

	var $refresherBlock = $(
			"<a href='#' onClick='refreshTable();return false;'>Обновить</a><br />" 
			+ "<input type='checkbox' id='autorefresh' onClick='autorefresh_click(this);' /> "
			+ "<span> Автоматически</span>"
		);
	$("th:last").html($refresherBlock);

	var $autorefresh = getAutorefresh();
	if ($autorefresh == 'no') {
		$autorefresh = 0;
	} else {
		$autorefresh = 1;
	}

	if ($autorefresh) {
		$("#autorefresh").attr('checked', 'checked');
		$refreshInterval = setTimeout(refreshTable, $refreshTableTimeout);
	}

	$("#crud_list_columns th.item0").text("№ заказа");
	$("#hide-controls").remove();

	$("th[name='created_at'], th[name='executed_at']").each(function() {
		var $self = $(this);
		var $class = $self.attr("class");
		//$("th."+$class+" input.crud_list_filter").change(refreshTable);
		$("th."+$class+" input.crud_list_filter").datepicker({
				dateFormat: 'yy-mm-dd'
			});
	});
	
	$("input").keydown(function(event) {
		if(event.keyCode==13) {
			return false;
		}
	});
	
	$(".chAll").live("click", function() {
		$(".reportColumns").attr('checked', 'checked');
		return false;
	});
	$(".unchAll").live("click", function() {
		$(".reportColumns").removeAttr('checked');
		return false;
	});
	
	

});
$(document).ready(function(){
	$("input.crud_list_filter").die('keyup', refreshTable);
	$("select.crud_list_filter").die('change', refreshTable);

	$('input.crud_list_filter').keydown(function(event){			
			switch(event.keyCode)
			{
				case 13:
				refreshTable();
				break;
			}

		});

	$('select.crud_list_filter').keydown(function(event){
			switch(event.keyCode)
			{
				case 13:
				refreshTable();
				break;
			}

		});
});


</script>

<style type="text/css">
input.crud_list_filter {
	width: 65px !important;
}

input.important_list_filter {
	width: 198px !important;
}

#report_block {
	padding: 20px;
}

#report_columns {
	padding-top: 10px;
}

.field_warning {
	border: 1px red solid;
}

</style>

<div id="search" style="display:none;">
<table id="m2mfilters">
	<tr>
		<th>Город</th>
		<th>Улица</th>
		<th>Корпус</th>
		<th>Дом</th>
		<th>Подъезд</th>
		<th>Этаж</th>
		<th>Квартира</th>
		<th>Метро</th>
	</tr>
	<tr>
		<td><input class='crud_list_filter' type='text' name='asearch[city]' /></td>
		<td><input class='crud_list_filter' type='text' name='asearch[street]' /></td>
		<td><input class='crud_list_filter' type='text' name='asearch[corps]' /></td>
		<td><input class='crud_list_filter' type='text' name='asearch[home]' /></td>
		<td><input class='crud_list_filter' type='text' name='asearch[porch]' /></td>
		<td><input class='crud_list_filter' type='text' name='asearch[level]' /></td>
		<td><input class='crud_list_filter' type='text' name='asearch[flat]' /></td>
		<td><input class='crud_list_filter' type='text' name='asearch[metro_id]' /></td>
	</tr>
</table>
<table id="m2mfilters">
	<tr>
		<th>E-mail пользователя</th>
		<th>Оператор КЦ (имя)</th>
		<th>Товар</th>
		<th>Дата доставки (от)</th>
		<th>Дата доставки (до)</th>
		<th>Дата оформления (от)</th>
		<th>Дата оформления (до)</th>
	</tr>
	<tr>
		<td><input class='crud_list_filter' type='text' name='asearch[email]' /></td>
		<td><input class='crud_list_filter' type='text' name='asearch[admin_user_id]' /></td>
		<td><input class='crud_list_filter important_list_filter' type='text' name='asearch[product]' /></td>
		<td><input class='crud_list_filter asdate' type='text' name='asearch[executed_at_from]' /></td>
		<td><input class='crud_list_filter asdate' type='text' name='asearch[executed_at_to]' /></td>
		<td><input class='crud_list_filter asdate' type='text' name='asearch[accepted_at_from]' /></td>
		<td><input class='crud_list_filter asdate' type='text' name='asearch[accepted_at_to]' /></td>
	</tr>
	
		
</table>
	<span id="query_list" onclick="refreshTable()" style="display: block; margin-left: 938px; background: #EEE; border: 1px solid #CCC; color: #000; font-weight: bold; padding: 5px; margin-bottom: 5px; width: 55px; cursor: pointer;">Выбрать</span>
</div>


<div id="report" style="display:none;">
<?php if($this->request['table'] != 'orders_price'){ ?>
	<div id="report_block">
		<a href="#" id="create_report">Сделать отчет</a>
			<div id="report_columns" style="display:none;">
				<input type='text' name='date_from' class="asdate" value=""/>  &nbsp;
			  <input type='text' name='date_to' class="asdate" value=""/><br>
			  
			  <input type='button' name='chAll' class="chAll" value="Выделить все"/>  &nbsp;
			  <input type='button' name='unchAll' class="unchAll" value="Убрать выделение"/><br><br>
			<?php 
				$columns = CallCenterService::getInstance()->reportColumns;
				foreach ($columns as $name => $value) {
					echo "<input type='checkbox' name='report[$name]' checked='checked' class='reportColumns'/> $value<br />";
				}
			?>
			
			<br />
			<input type='hidden' name='report[id]' value='1' />
			<input type='submit' name='create_report' value='Скачать' />
		</div>
	</div>
<?php }else{ ?>
		<script type="text/javascript">
			$('.nav-item').hide();
		</script>
<?php } ?>
</div>

